package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.R
import com.youdianstar.app.entity.PointPlan




/**
 * File: PointPlanAdapter
 * Author: 82149 Create: 2022/3/3 16:15
 * Changes (from 2022/3/3)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class PointPlanAdapter(val context: Context, val data: MutableList<PointPlan>,val listener:IActionListener)
    : RecyclerView.Adapter<PointPlanAdapter.PlanHolder>() {

    interface IActionListener{
        fun onItemAction(type:Int)
    }

    class PlanHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val container = itemView.findViewById<ConstraintLayout>(R.id.cl_container_plan)

        val tvTitle = itemView.findViewById<TextView>(R.id.item_tv_title)
        val tvPoints = itemView.findViewById<TextView>(R.id.item_tv_points)
        val tvInfo = itemView.findViewById<TextView>(R.id.item_content)

        val tvState = itemView.findViewById<TextView>(R.id.item_tv_state)
        val btnAction = itemView.findViewById<TextView>(R.id.btn_action)
    }

    private var languageNow :String = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
    }

    fun update(list:List<PointPlan>){
        data.clear()
        data.addAll(list)
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlanHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_point_plan, parent, false)
        val holder = PlanHolder(view)
        return holder
    }

    override fun onBindViewHolder(holder: PlanHolder, position: Int) {
        data[position].let { plan ->
            when(languageNow){
                Language.ES->{
                    holder.tvTitle.text = plan.title.es +"\n("+plan.description.es+")"
                }
                Language.ZH->{
                    holder.tvTitle.text = plan.title.zh +"\n("+plan.description.zh+")"
                }
                else ->{
                    holder.tvTitle.text = plan.title.en +"\n("+plan.description.en+")"
                }
            }
            holder.tvPoints.text = String.format(context.getString(R.string.Public_form_content),context.getString(R.string.points_points),plan.point.toString())

            //state : 1-上架（有效时间） 2-永久上架 3-下架
            //type : 1-新用户注册 2-每日访问APP 3-租借 4-绑卡 5-报失
            when(plan.type){
                1->{
                    holder.btnAction.visibility = View.INVISIBLE
                    holder.tvState.visibility = View.VISIBLE
                }
                2->{
                    holder.btnAction.visibility = View.INVISIBLE
                    holder.tvState.visibility = View.VISIBLE
                }
                3->{
                    holder.btnAction.visibility = View.VISIBLE
                    holder.tvState.visibility = View.INVISIBLE
                    holder.btnAction.text = context.getString(R.string.points_gotoRent)
                }
                4->{
                    holder.btnAction.visibility = if (plan.actionState) View.VISIBLE else View.INVISIBLE
                    holder.tvState.visibility = View.INVISIBLE
                    holder.btnAction.text = context.getString(R.string.points_addNow)
                }
                else->{
                    holder.btnAction.visibility = View.INVISIBLE
                    holder.tvState.visibility = View.INVISIBLE
                }
            }
        }

        holder.btnAction.setThrottleListener {
            listener.onItemAction(data[holder.bindingAdapterPosition].type)
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    fun updatePlanItem(type:Int, isEnable:Boolean){
        for ((index,item) in data.withIndex()){
            if (item.type == type){
                item.actionState = isEnable
                notifyItemChanged(index)
            }
        }
    }
}